package cn.xueden.system.service.impl;


import cn.xueden.system.mapper.AlipayMapper;
import cn.xueden.system.model.entity.Alipay;
import cn.xueden.system.service.IAlipayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**功能描述： 支付宝服务实现类
 * @author:梁志杰
 * @date:2025/6/10
 * @description:cn.xueden.system.service.impl
 * @version:1.0
 */
@Service
@RequiredArgsConstructor
public class AlipayServiceImpl extends ServiceImpl<AlipayMapper, Alipay> implements IAlipayService {

    /**
     * 分页获取支付宝列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    @Cacheable(value = "alipayList",key = "#pageNo+'_'+#pageSize",
    unless = "#result.records.size()==0")
    public Page<Alipay> getPageList(Integer pageNo, Integer pageSize) {
        Page<Alipay> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Alipay> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        return this.page(page,wrapper);
    }

    /**
     * 创建支付宝
     * @param alipay
     * @return
     */
    @Override
    @CacheEvict(value = "alipayList",allEntries = true)
    public Boolean create(Alipay alipay) {
        // 新增默认禁用状态
        alipay.setStatus(1);
        return this.save(alipay);
    }

    /**
     * 更新支付宝
     * @param alipay
     * @return
     */
    @Override
    @CacheEvict(value = "alipayList",allEntries = true)
    public boolean updateAlipayById(Alipay alipay) {
        return this.updateById(alipay);
    }

    /**
     * 删除支付宝
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "alipayList",allEntries = true)
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }

    /**
     * 切换支付宝状态
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "alipayList",allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public boolean switchStatusWithCheck(Integer id) {
        // 1. 获取当前有效配置（包含逻辑删除校验）
        Alipay current = getOne(new LambdaQueryWrapper<Alipay>()
                .eq(Alipay::getId, id)
                .eq(Alipay::getIsDeleted, 0));

        if (current == null) {
            throw new RuntimeException("支付配置不存在或已被删除");
        }

        // 2. 如果目标状态是启用，说明正确执行禁用操作
        if (current.getStatus() == 0) {
            // 2.2 启用当前配置
            current.setStatus(1);
            return updateById(current);
            // 3. 如果目标状态是禁用，正在执行启用操作，其他状态需要改为禁用，确保有且仅有一条是启用
        }else {
            // 3.1 禁用所有其他有效配置
            update(new LambdaUpdateWrapper<Alipay>()
                    .set(Alipay::getStatus, 1)
                    .ne(Alipay::getId, id)
                    .eq(Alipay::getIsDeleted, 0));
            // 3.2 启用当前配置
            current.setStatus(0);
            return updateById(current);
        }
    }
}
